﻿var lastSelectedListId = null;

function LQWP_LaunchListPicker(clientId, serverUrl) {

    var callback = function(results) {
        LQWP_SetList(clientId, results);
    };

    LaunchPickerTreeDialog('CbqPickerSelectListTitle', 'CbqPickerSelectListText', 'listsOnly', '', serverUrl, lastSelectedListId, '', '', '/_layouts/images/smt_icon.gif', '', callback);
}

function LQWP_SetList(clientId, results) {
    var listTextBox = document.getElementById(clientId);

    if (results == null
        || results[1] == null
        || results[2] == null) return;

    if (results[2] == "") {
        alert("You must select a list!.");
        return;
    }

    lastSelectedListId = results[0];
    var listUrl = '';
    if (listUrl.substring(listUrl.length - 1) != '/')
        listUrl = listUrl + '/';
    if (results[1].charAt(0) == '/')
        results[1] = results[1].substring(1);
    listUrl = listUrl + results[1];
    if (listUrl.substring(listUrl.length - 1) != '/')
        listUrl = listUrl + '/';
    if (results[2].charAt(0) == '/')
        results[2] = results[2].substring(1);
    listUrl = listUrl + results[2];
    listTextBox.value = listUrl;
}

function LQWP_LaunchQueryBuilder(listClientId, queryClientId, serverUrl) {

    var queryTextBox = document.getElementById(queryClientId);
    var listTextBox = document.getElementById(listClientId);

    if (listTextBox.value.replace(/^\s+|\s+$/g, "") == "") {
        alert("A list must be selected before building a query.");
        return;
    }

    var callback = function(results) {
    LQWP_SetQuery(queryClientId, results);
    };

    var query = queryTextBox.value;
    query = Mullivan.Utilities.UrlEncode(query);

    QB_LaunchQueryBuilderDialog(serverUrl, listTextBox.value, null, query, false, true, true, callback);
}

function LQWP_SetQuery(clientId, results) {
    var queryTextBox = document.getElementById(clientId);

    if (results == null || results.length < 2)
        return;

    //Index 1 contains the query and 0 is for the view if it is being used.
    queryTextBox.value = results[1];
}

function LQWP_LaunchViewBuilder(listClientId, viewClientId, serverUrl) {

    var viewTextBox = document.getElementById(viewClientId);
    var listTextBox = document.getElementById(listClientId);

    if (listTextBox.value.replace(/^\s+|\s+$/g, "") == "") {
        alert("A list must be selected before building a query.");
        return;
    }

    var callback = function(results) {
    LQWP_SetView(viewClientId, results);
    };

    var view = viewTextBox.value;
    view = Mullivan.Utilities.UrlEncode(view);

    QB_LaunchQueryBuilderDialog(serverUrl, listTextBox.value, view, null, true, false, false, callback);
}

function LQWP_SetView(clientId, results) {
    var viewTextBox = document.getElementById(clientId);

    if (results == null || results.length < 2)
        return;

    //Index 1 contains the query and 0 is for the view if it is being used.
    viewTextBox.value = results[0];
}